<?php
require_once $_SERVER["DOCUMENT_ROOT"] . "/config/globalconfig.php";
require_once $_SERVER["DOCUMENT_ROOT"] . "/public/Admin.php";
require_once $_SERVER["DOCUMENT_ROOT"] . "/public/Attachment.php";
$admin = new Admin();
$action = $_REQUEST["action"];
$condition = array();
$attach = new Attachment();
$reda;
$message = "success";
$code = 200;
$is_login = false;
try {
    switch ($action) {
        case LOAD:
            $reda = $admin->findOne();
            break;
        case LOAD_ID:
            $condition[$admin->getPrimaryKey()] = $_GET["id"];
            $reda = $admin->findOne($condition);
            break;
        case UPLOAD:
            $reda = $attach->uploadWithoutSave($action, "public");
            break;
        case "html_upload":
            $reda = $attach->uploadWithoutSave($action, "public", "file");
            break;
        case ALL:
            $reda = $admin->findList();
            break;
        case PAGE:
            $page = $_REQUEST["currentPage"];
            $pageSize = $_REQUEST["pageSize"];
            $condition = $admin->searchData();
            $reda = $admin->findByPage($page, $pageSize, $condition);
            break;
        case DELETE:
            $ids = $_POST["ids"];
            $condition[$admin->getPrimaryKey()] = [$admin->param_operation => "in", $admin->param_value_key => $ids];
            $reda = $admin->delete($condition);
            break;
        case MERGE:
            $data = $admin->parseRequestData();
            $reda = $admin->merge($admin->getPrimaryKey(), $data);
            break;
        case LOGIN:
            $reda = $admin->login($_POST["userName"], $_POST["password"], $_POST["code"]);
            if ($reda < 1) {
                $code = 500;
                $reda = $reda == -1 ? "验证码错误" : "用户名或者密码错误";
                $message = $reda;
            }
            $is_login = true;
            break;
        case LOGOUT:
            $admin->logout($_GET["token"]);
            break;
        case STATUS:
            $statusType = $_POST["statusType"];
            $reda = $admin->statusList($statusType);
            break;
        case PERMISSION:
            $reda = $admin->permission($_GET["userId"]);
            break;
        default:
            $reda = "未知Action";
    }
    if ($action == "html_upload") {
        echo json_encode(["location" => $reda["path"]]);
    } else {

        $admin->result($reda, $message, $code, $is_login);
    }
} catch (Exception $e) {
    $admin->result($e->getMessage(), $admin->error_msg, $admin->error_code);
}


?>
